class Stock {
  constructor(symbol, name, currentPrice, lastUpdated) {
    this.symbol = symbol;           // 股票代码
    this.name = name;               // 股票名称
    this.currentPrice = currentPrice; // 当前价格
    this.lastUpdated = lastUpdated;   // 最后更新时间
    this.history = [];              // 历史价格数据
  }

  // 更新股票价格
  updatePrice(newPrice) {
    this.history.push({
      price: this.currentPrice,
      timestamp: this.lastUpdated
    });
    
    this.currentPrice = newPrice;
    this.lastUpdated = new Date();
  }

  // 获取价格历史
  getPriceHistory() {
    return this.history;
  }

  // 计算价格变化百分比
  getPriceChangePercentage() {
    if (this.history.length === 0) return 0;
    
    const previousPrice = this.history[this.history.length - 1].price;
    return ((this.currentPrice - previousPrice) / previousPrice) * 100;
  }

  // 获取股票信息
  getInfo() {
    return {
      symbol: this.symbol,
      name: this.name,
      currentPrice: this.currentPrice,
      lastUpdated: this.lastUpdated,
      priceChangePercentage: this.getPriceChangePercentage()
    };
  }
}

module.exports = Stock;